/*
 * 字典类型管理 API 接口
 * 提供字典类型的增删改查等操作接口
 */

import { flags } from './data';
import { 
  getMockSysDictTypeData, 
  mockAddSysDictType, 
  mockEditSysDictType, 
  mockDeleteSysDictType, 
  mockFetchSysDictTypeDetail,
  mockUpdateSysDictTypeStatus,
  mockUpdateSysDictTypeSortOrder
} from './mock';
import type { SysDictType } from './types';

// 导入配置函数
import { 
  setUseMockData, 
  setShowTestButtonOnListPage,
  getApiConfig,
  showTestButtonOnListPage
} from './auto/generated-api';

// 重新导出配置函数
export {
  setUseMockData,
  getApiConfig,
  showTestButtonOnListPage,
  setShowTestButtonOnListPage
} from './auto/generated-api';

// 根据配置设置Mock模式
setUseMockData(flags.enableMockDataMode);
setShowTestButtonOnListPage(flags.showTestButtonOnListPage);

// 创建适配层API函数来解决auto文件参数不匹配问题

// 获取字典类型列表 - 适配层
export async function apiSysDictTypeFetchList(params: any) {
  if (flags.enableMockDataMode) {
    // 转换参数格式以匹配Mock函数
    const mockParams = {
      current: params.pageNumber || params.current || 1,
      pageSize: params.pageSize || 10,
      dictName: params.dictName || params.name,
      dictType: params.dictType || params.studentNo,
      status: params.status
    };
    
    return new Promise((resolve) => {
      setTimeout(() => {
        const result = getMockSysDictTypeData(mockParams);
        resolve({ success: true, data: result });
      }, 300);
    });
  } else {
    // 真实API调用
    const { anyGet } = await import('@/network');
    return anyGet('/api/anon/sysdicttype/manage', params);
  }
}

// 新增字典类型 - 适配层
export async function apiSysDictTypeAdd(data: Partial<SysDictType>) {
  if (flags.enableMockDataMode) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockAddSysDictType(data));
      }, 300);
    });
  } else {
    // 真实API调用
    const { anyPost } = await import('@/network');
    return anyPost('/api/anon/sysdicttype/manage', data);
  }
}

// 编辑字典类型 - 适配层
export async function apiSysDictTypeEdit(data: Partial<SysDictType>) {
  if (flags.enableMockDataMode) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockEditSysDictType(data.id!, data));
      }, 300);
    });
  } else {
    // 真实API调用
    const { anyPut } = await import('@/network');
    return anyPut(`/api/anon/sysdicttype/manage/${data.id}`, data);
  }
}

// 删除字典类型 - 适配层
export async function apiSysDictTypeDelete(id: number) {
  if (flags.enableMockDataMode) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockDeleteSysDictType(id));
      }, 300);
    });
  } else {
    // 真实API调用
    const { anyDelete } = await import('@/network');
    return anyDelete(`/api/anon/sysdicttype/manage/${id}`);
  }
}

// 获取字典类型详情 - 适配层
export async function apiSysDictTypeFetchDetail(id: number) {
  if (flags.enableMockDataMode) {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockFetchSysDictTypeDetail(id));
      }, 300);
    });
  } else {
    // 真实API调用
    const { anyGet } = await import('@/network');
    return anyGet(`/api/anon/sysdicttype/manage/${id}`);
  }
}

// 新增API：更新字典类型状态
export async function apiSysDictTypeUpdateStatus(id: number, status: number) {
  if (flags.enableMockDataMode) {
    // Mock模式
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockUpdateSysDictTypeStatus(id, status));
      }, 300);
    });
  } else {
    // 真实API调用
    const { anyPut } = await import('@/network');
    return anyPut(`/api/anon/sysdicttype/manage/${id}/status?status=${status}`);
  }
}

// 新增API：更新字典类型排序
export async function apiSysDictTypeUpdateSortOrder(id: number, sortOrder: number) {
  if (flags.enableMockDataMode) {
    // Mock模式
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve(mockUpdateSysDictTypeSortOrder(id, sortOrder));
      }, 300);
    });
  } else {
    // 真实API调用
    const { anyPut } = await import('@/network');
    return anyPut(`/api/anon/sysdicttype/manage/${id}`, { sortOrder });
  }
}